package com.inhim.pjhim.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.inhim.pjhim.R;
import com.inhim.pjhim.adapter.AllCateAdapter;
import com.inhim.pjhim.entity.ReaderTypeList;
import com.inhim.pjhim.http.MyOkHttpClient;
import com.inhim.pjhim.http.Urls;
import com.inhim.pjhim.view.BToast;

import org.yczbj.ycrefreshviewlib.view.YCRefreshView;

import java.io.IOException;
import java.util.HashMap;

import okhttp3.Request;

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public class AlllCateFragment extends Fragment {
    private Gson gson;
    private int totalPage;
    private AllCateAdapter mAdapter;
    private int mPageNum = 1;
    private Boolean refresh = true;
    private String TAG="AlllCateFragment";
    private YCRefreshView ycRefreshView;
    private Activity mContext;
    private String Type,Title;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_alll_cate, container, false);
        gson = new Gson();
        mContext=getActivity();
        Type=getArguments().getString("Type");
        Title=getArguments().getString("Title");
        initView(view);
        initAdapter();
        getReaderTypeList();
        return view;
    }

    private void initView(View view) {
        ycRefreshView=view.findViewById(R.id.ycRefreshView);
    }

    private void getReaderTypeList() {
        /** {
         "readerStyleId": "string",
         "readerStyleValueId": "string",
         "readerTypeId": "string",
         "title": "string"
         }*/
        HashMap postMap = new HashMap();
        String url= Urls.getReaderTypeList(mPageNum, 8, Type,0);
        MyOkHttpClient.getInstance().asyncJsonPost(url, postMap, new MyOkHttpClient.HttpCallBack() {
            @Override
            public void onError(Request request, IOException e) {
                BToast.showText(mContext.getResources().getString(R.string.get_date_error), false);
            }

            @Override
            public void onSuccess(Request request, String result) {
                try{
                    ReaderTypeList readerTypeList = gson.fromJson(result, ReaderTypeList.class);
                    totalPage=readerTypeList.getPage().getTotalPage();
                    if(refresh){
                        mAdapter.clear();
                    }
                    if (readerTypeList.getCode() == 0&&readerTypeList.getPage().getList().size()>0) {
                        for (int i = 0; i < readerTypeList.getPage().getList().size(); i++) {
                            mAdapter.add(readerTypeList.getPage().getList().get(i));
                        }
                    } else if(readerTypeList.getCode() != 0){
                        BToast.showText(readerTypeList.getMsg(), false);
                    }
                    if(mPageNum>=totalPage){
                        mAdapter.pauseMore();
                    }
                    mAdapter.notifyDataSetChanged();
                }catch (Exception e){
                    mAdapter.notifyDataSetChanged();
                    e.printStackTrace();
                }
            }
        });
    }


    private void initAdapter() {
        mAdapter = new AllCateAdapter(mContext,Title,Type);
        ycRefreshView.setLayoutManager(new LinearLayoutManager(mContext));
        ycRefreshView.setEmptyView(R.layout.empty_view_layout);
        ycRefreshView.setAdapter(mAdapter);
        // 设置布局管理
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 3);
        ycRefreshView.setLayoutManager(manager);
        //设置上拉加载更多时布局，以及监听事件
        mAdapter.setMore(R.layout.view_more, () -> {
            //可以做请求下一页操作
            mPageNum++;
            refresh=false;
            getReaderTypeList();
        });
        ycRefreshView.setRefreshListener(() -> {
            mPageNum= 1;
            refresh=true;
            getReaderTypeList();
        });
        /*mAdapter.setOnItemClickListener(position -> {
            ReaderTypeList.List reader=mAdapter.getAllData().get(position);
            Intent intent=new Intent(mContext, TypeListActivity.class);
            intent.putExtra("TAG","ReadingTwoFragment");
            intent.putExtra("ReaderTypeList",reader);
            mContext.startActivity(intent);
        });*/
        ycRefreshView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                final LinearLayoutManager layoutManager = (LinearLayoutManager) ycRefreshView.getRecyclerView().getLayoutManager();
                int pos =layoutManager.findFirstCompletelyVisibleItemPosition();//获取第一个先是的View的索引
            }
        });
    }
}
